package com.shqcjd.sinopecdemomobile.ui.activity;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.shqcjd.sinopecdemomobile.R;
import com.shqcjd.sinopecdemomobile.api.ApiService;
import com.shqcjd.sinopecdemomobile.helper.rxjavahelper.RxObserver;
import com.shqcjd.sinopecdemomobile.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.sinopecdemomobile.model.ResponseData;
import com.shqcjd.sinopecdemomobile.model.pojo.EquipmentGroupBean;
import com.shqcjd.sinopecdemomobile.ui.adapter.StateInspectionRecordAdapter;
import com.shqcjd.sinopecdemomobile.ui.base.BaseActivity;
import com.shqcjd.sinopecdemomobile.ui.base.BasePresenter;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

public class StateInspectionRecordActivity extends BaseActivity {

    @BindView(R.id.tv_include_title)
    TextView tvIncludeTitle;
    @BindView(R.id.tv_include_finish)
    Button tvIncludeFinish;
    @BindView(R.id.tv_state_record_name)
    TextView tvStateRecordName;
    @BindView(R.id.tv_state_record_state)
    TextView tvStateRecordState;
    @BindView(R.id.rv_state_record_group)
    RecyclerView rvStateRecordGroup;

    private Context context;
    private StateInspectionRecordAdapter stateInspectionRecordAdapter;
    private int tId;
    private String kks;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_state_inspection;
    }

    @Override
    public void init() {
        super.init();
        context = this;
                kks = getIntent().getStringExtra("kks");
        tId = getIntent().getIntExtra("tId", 1);
    }

    @Override
    public void initView() {
        super.initView();
        tvIncludeTitle.setText("巡检记录");
        tvIncludeFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        getData();
    }

    private void getData() {
        ApiService.equipmentGroupHttp(tId, kks.replace("#", "%23"))
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<EquipmentGroupBean>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                        showWaitingDialog("正在加载...");
                    }

                    @Override
                    public void _onNext(ResponseData<EquipmentGroupBean> responseData) {
                        if(responseData.retCode!=10000){
                            Toast.makeText(context, responseData.retMsg, Toast.LENGTH_SHORT).show();
                            return;
                        }
                        tvStateRecordName.setText(responseData.data.equipment_check_list.equipment_name);
                        if(responseData.data.equipment_check_list.check_normal == 1){
                            tvStateRecordState.setText("正常");
                            tvStateRecordState.setTextColor(Color.parseColor("#363636"));
                        }else if(responseData.data.equipment_check_list.check_normal == 2){
                            tvStateRecordState.setText("异常");
                            tvStateRecordState.setTextColor(Color.parseColor("#E12B2B"));
                        }else if(responseData.data.equipment_check_list.check_normal == 0){
                            tvStateRecordState.setText("未巡检");
                            tvStateRecordState.setTextColor(Color.parseColor("#8e9296"));
                        }


                        rvStateRecordGroup.setLayoutManager(new LinearLayoutManager(context));
                        stateInspectionRecordAdapter = new StateInspectionRecordAdapter(responseData.data.equipment_check_list.equipment_children,context);
                        rvStateRecordGroup.setAdapter(stateInspectionRecordAdapter);
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        Log.e("tou", errorMessage);
                        hideWaitingDialog();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        hideWaitingDialog();
                    }

                });
    }

}
